<?php

    function getSOAPDate($timeStamp, $withTime = false)
    {
        $soapDate = date("Y-m-d", $timeStamp);
        return ($withTime) ? $soapDate."T".date("H:i:s", $timeStamp) : $soapDate."T00:00:00";
    }
    
    function getSoapServer()
    {
        global $mikron;
        $servers = $mikron->Queries->Query("T_VARIABLES", null, new Criterion('code', 'SOAPSERVER'));
        if($servers->count() > 0)
        {
            return $servers->fetch()->textvalue->value;
        }
    }

    // $soap = new SoapClient("http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx?WSDL");
    // $params["On_date"] = time(); //getSOAPDate(time());
    // var_dump($soap->GetCursOnDateXML($params));
    // echo cnv($soap->GetCursOnDateXML($params)->GetCursOnDateXMLResult->any);   

    function cnv($text)
    {
        return iconv("UTF-8", "WINDOWS-1251", $text);
    }

    function cnv_to($text)
    {
        return iconv("WINDOWS-1251", "UTF-8", $text);
    }

    function ArrayToXML($a, $MethodName)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("method");
        $root->setAttribute('methodname', $MethodName);
        foreach($a as $key => $value)
        {
            $field = $doc->createElement("field");   
            $field->setAttribute('name', $key);
            $field->setAttribute('value', $value);
            $root->appendChild($field);
        }
        $doc->appendChild($root);        
        return $doc->SaveXML();
    }

    function ServerFunction($function, $params = null)
    {
        global $mikron;
        if(is_array($params))
        {
            $params = ArrayToXML($params, $function);
        }
        $retr = CallSoap($function, $params);
        if(is_null($retr->XML))
        {
            $entityname = "T_".strtoupper($retr->MethodName);
            $mikron->EntityManager->ShowServerForm(new $entityname, null, false, $retr->MethodName);
        }
        else
        {
            switch($retr->MethodName)
            {
                // ответ аутентификации
                case "authentification":
                    $dom1 = new DomDocument();
                    $dom1->loadXML($retr->XML);
                    $authentification = $dom1->getElementsByTagName("authentification");
                    foreach($authentification as $item)
                    {
                        if($item->hasAttribute('guid'))
                        {
                             $guid = $item->getAttribute('guid');
                             if(!is_null($guid))
                             {
                                $_SESSION['guid'] = $guid;
                                break;
                             }
                        }
                    }
                    break;
            }
        }
    }

    function CallSoap($function, $parameters, $server = null)
    {
        if(is_null($server))
        {
            $server = getSoapServer();
            if(is_null($server))
            {
                CriticalError("Soap server is not set.", null, true);
            }
        }
        $soap = new SoapClient($server);
        $params = Array("MethodName" => $function, "XML" => $parameters);
        if(array_key_exists("guid", $_SESSION))
        {
            $params["SessionID"] = $_SESSION['guid'];
        }
        else
        {
            $params["SessionID"] = null;
        }
        $resp = $soap->Operation($params);
        return $resp;
    }

?>